const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// 设置nodejs环境变量
// process.env.NODE_ENV = 'development';

/** @type { import('webpack').Configuration } */
const config = {
    entry: './src/js/index.js',
    output: {
        filename: 'js/built.js',
        path: resolve(__dirname, 'build')
    },
    module: {
        rules: [
            {
                test: /\.less$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'less-loader',
                    /*
                    css兼容性处理：postcss --> postcss-loader postcss-preset-env
                    帮postcss找到package.json中browserslist里面的配置，通过配置加载指定的css兼容性样式
                    "browserslist": {
                        // 开发环境 --> 设置node环境变量：process.env.NODE_ENV = development
                        "development": [
                            "last 1 chrome version",
                            "last 1 firefox version",
                            "last 1 safari version"
                        ],
                        // 生产环境：默认是看生产环境
                        "production": [
                            ">0.2%",
                            "not dead",
                            "not op_mini all"
                        ]
                    }
                    */
                    // 自定义loader配置
                    {
                        loader: 'postcss-loader',
                        options: {
                            ident: 'postcss',
                            plugins: () => [
                                // postcss的插件
                                require('postcss-preset-env')()
                            ]
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        new MiniCssExtractPlugin({
            filename: 'css/style.css'
        }),
        new CleanWebpackPlugin()
    ],
    mode: 'development'
};

module.exports = config;
